package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.cmd.orewar.task.OrePitRecallTask;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_RECALL_PIT, desc = "撤回矿坑")
public class OrePitRecallCmd extends AbstractCommand {

	private int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.OrePitWithdrawReqMsg reqMsg = OreWarProto.OrePitWithdrawReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		ActivityInfo activityInfo = OreWarActivityMgr.getInTimeActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!OreWarMgr.isActivityActive(activityId)){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_DAY_TIME;
			return;
		}
		if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
			OreWarMgr.addTask(activityId,new OrePitRecallTask(packet, player));
		}else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
			int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
			if (groupId == UnionActivityGroupHelper.NO_GROUP_ID){
				ret = GameErrorCode.E_ORE_WAR_CROSS_NO_GROUP;
				return;
			}

			OreWarProto.CrossOrePitRecallReqMsg.Builder msg = OreWarProto.CrossOrePitRecallReqMsg.newBuilder();
			msg.setActivityId(reqMsg.getActivityId());
			msg.setPitId(reqMsg.getPitId());
			msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
			msg.setGroupId(groupId);
			GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_OREWAR_RECALL,msg,player.getUserId(),activityId,groupId);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		OreWarProto.OrePitWithdrawRespMsg.Builder resMsg =  OreWarProto.OrePitWithdrawRespMsg.newBuilder();
		resMsg.setRet(ret);
		if(ret != 0){
			player.sendPacket(Protocol.U_ORE_WAR_RECALL_PIT, resMsg);
		}
	}
}
